package com.zxy.salenet.web.web.controller;

import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.pojo.SalenetComm;
import com.zxy.salenet.web.pojo.SalenetIndent;
import com.zxy.salenet.web.pojo.SalenetUser;
import com.zxy.salenet.web.pojo.dto.UpdateCommDTO;
import com.zxy.salenet.web.pojo.vo.UserMessageVO;
import com.zxy.salenet.web.service.UserConfigService;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author: zxy
 * @version: 1.0
 * @description: 用户设置
 */
   /**
 * UserConfigController.
 */
@Api(value = "用户个人设置API",tags = "用户个人设置API")
@RestController
@RequestMapping(path = "/userConfig")
public class UserConfigController {
    @Autowired
    private UserConfigService userConfigService;

    /**
     * 获取当前登录用户的所有信息
     *
     * @return
     */
    @ApiOperation(value = "用户个人设置 获取当前登陆人的所有的信息",notes = "发送当前登陆的用户的userId和用户名称userName 数据格式JSON {\"userId\":67531,\"userName\":\"韦德de忠实粉丝\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/userMessage")
    private Json queryUserAllMessage(@RequestBody Map<String, String> map) {
        Json result = new Json();
        Integer userId = Integer.parseInt(map.get("userId"));
        String userName = map.get("userName");
        UserMessageVO userMessageVO = userConfigService.selectUserMessageAll(userId, userName);
        result.setData(userMessageVO);
        result.setCode(200);
        result.setMessage("查询成功");
        return result;
    }

    /**
     * 修改用户信息
     *
     * @return
     */
    @ApiOperation(value = "用户修改自己的信息",notes = "用户修改自己的信息  密码除外 JSON 格式 {    \"id\":1,\"userId\":67531,\"userName\":\"韦德de忠实粉丝\",\n" +
            "    \"userRealName\":\"李珏\"\n" +
            "    \"userSex\":\"男\",\n" +
            "    \"userAddress\":\"河南省郑州市金水区金碧苑3301\",\n" +
            "    \"userBrith\":\"1998-02-02\",\n" +
            "    \"userPasswd\":\"199802..\",\n" +
            "    \"userPhone\":\"13097814606\",\n" +
            "    \"userEmail\":\"123@qq.com\",\n" +
            "    \"userPaypass\":\"123456\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/userModify")
    private Json modifyUserMessage(@RequestBody SalenetUser salenetUser) {
        Json result = new Json();
        System.out.println(salenetUser.toString());
        Integer integer = userConfigService.updateUserMessage(salenetUser);
        if(integer.equals(0)){
            result.setCode(220);
            result.setData(0);
            result.setMessage("修改失败");
            return result;
        }
        result.setCode(200);
        result.setData(1);
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 修改用户密码
     *
     * @return
     */
    @ApiOperation(value = "用户修改自己的密码  可以让用户自己输入自己的用户名和支付密码 也可以直接发送用户名 如果支付密码正确则进行修复 如果错误则修改失败",notes = "{\"userId\":67531" +
            "\"userPayPass\":000000\n" +
            "\"newUserpassword\":\"test123..\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/modifyUserPass")
    private Json modifyUserPassword(@RequestBody Map<String, String> map) {
        Json json = new Json();
        Integer userId = Integer.parseInt(map.get("userId"));
        String userPayPass = map.get("userPayPass");
        String newUerPassword = map.get("newUerPassword");
        Integer result = userConfigService.updateUserPass(userId, userPayPass, newUerPassword);
        if (result == -1) {
            json.setCode(700);
            json.setData(result);
            json.setMessage("支付密码错误");
        }
        json.setCode(200);
        json.setData(result);
        json.setMessage("修改成功");
        return json;
    }

    /**
     * 新增常用收货地址
     *
     * @param salenetComm
     * @return
     */
    @ApiOperation(value = "用户添加自己常用收货地址",notes = "添加常用收货地址  JSON格式数据 {\n" +
            "    \"commUserId\":\"85044\",\n" +
            "    \"commProvince\":\"广东省\",\n" +
            "    \"commCity\":\"广州市\",\n" +
            "    \"commCounty\":\"天河区\",\n" +
            "    \"commVillage\":\"\",\n" +
            "    \"commAddress\":\"春江水岸3栋201\",\n" +
            "    \"commUsername\":\"史迪仔\",\n" +
            "    \"commPhone\":\"18745668977\"\n" +
            "}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/addCurrComm")
    private Json addCurrComm(@RequestBody SalenetComm salenetComm) {
        Json result = new Json();
        Integer addResult = userConfigService.addCurrAddComm(salenetComm);
        if (addResult != 0) {
            result.setCode(200);
            result.setData(1);
            result.setMessage("添加成功");
            return result;
        }
        result.setCode(800);
        result.setData(0);
        result.setMessage("添加错误");
        return result;
    }

    /**
     * 删除常用地址
     *
     * @return
     */
    @ApiOperation(value = "根据sid删除对应的收货地址",notes = "根据收货地址的id删除收货地址 JSON {\"id\":1}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping("/deleteComm")
    private Json deleteComm(Map<String, Integer> map) {
        Json json = new Json();
        Integer sid = map.get("id");
        Integer deleteResult = userConfigService.deleteCurrComm(sid);
        if (deleteResult != 0) {
            json.setCode(200);
            json.setData(1);
            json.setMessage("删除成功");
            return json;
        }
        json.setCode(800);
        json.setData(0);
        json.setMessage("删除失败");
        return json;
    }

    /**
     * 更改个人收货地址
     * @return
     */
    @ApiOperation(value = "修改个人主要收货地址",notes = "修改用户购买商品时选择的收货地址 JSON格式  {\n" +
            "    \"userId\":\"67531\",\n" +
            "    \"commId\":\"2\"\n" +
            "}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/updatePublicComm")
    private Json modifyPublicComm(@RequestBody UpdateCommDTO commDTO){
        Json json = new Json();
        boolean success = userConfigService.updatePublicComm(commDTO);
        if(success){
            json.setMessage("选择成功");
            json.setCode(200);
            json.setData(success);
            return json;
        }
        json.setMessage("选择失败");
        json.setCode(220);
        json.setData(success);
        return json;
    }

    /**
     * 更新个人地址
     *
     * @param comm
     * @return
     */
    @ApiOperation(value = "修改自己的收货地址",notes = "根据现有的数据修改自己的地址 JSON格式 {\n" +
            "    \"id\":4,\n" +
            "    \"commUserId\":\"85044\",\n" +
            "    \"commProvince\":\"广东省\",\n" +
            "    \"commCity\":\"广州市\",\n" +
            "    \"commCounty\":\"天河区\",\n" +
            "    \"commVillage\":\"\",\n" +
            "    \"commAddress\":\"春风十里3栋201\",\n" +
            "    \"commUsername\":\"史迪仔\",\n" +
            "    \"commPhone\":\"18745668977\"\n" +
            "}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "updateComm")
    private Json updateComm(@RequestBody SalenetComm comm) {
        Json json = new Json();
        Integer updateResult = userConfigService.updateComm(comm);
        if (updateResult != 0) {
            json.setCode(200);
            json.setData(1);
            json.setMessage("修改成功");
            return json;
        }
        json.setCode(800);
        json.setData(0);
        json.setMessage("修改失败");
        return json;
    }

    /**
     * 获取用户购买记录
     *
     * @return
     */
    @ApiOperation(value = "获取当前用户的购买记录 发送用户的userId pageNum当前页 pageSize一页显示多少数据",notes = "获取当前用户的购买记录 {\"userId\":67531,\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/queryAllBuyLog")
    private Json queryAllBuyLog(@RequestBody Map<String, Integer> map) {
        Json json = new Json();
        Integer userId = map.get("userId");
        Map<String, List<SalenetIndent>> indentPage = userConfigService.queryAllBuyLog(userId);
        json.setCode(200);
        json.setData(indentPage);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 删除对应购买记录
     *
     * @return
     */
    @ApiOperation(value = "用户删除自己的购买记录 目前还没有购买记录",notes = "购买记录的 indentId,userId {\"indentId\":1,\"userId\":67531}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/deleteGoodBuyLog")
    private Json deleteGoodBuyLog(@RequestBody Map<String,Integer> map) {
        Json json = new Json();
        Integer indentId = map.get("indentId");
        Integer userId = map.get("userId");
        Integer deleteResult = userConfigService.deleteBuyLog(indentId, userId);
        json.setData(deleteResult);
        json.setCode(200);
        json.setMessage("删除成功");
        return json;
    }
}
